package com.datawin.taotaocheche.ui.fragment.presenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import com.datawin.taotaocheche.base.BaseBean;
import com.datawin.taotaocheche.base.BaseBiz;
import com.datawin.taotaocheche.bean.ClueInfo;
import com.datawin.taotaocheche.bean.SettingInfo;
import com.datawin.taotaocheche.comm.Constants;
import com.datawin.taotaocheche.comm.MyApplication;
import com.datawin.taotaocheche.ui.actviity.login.SelcetInfoAct;
import com.datawin.taotaocheche.ui.actviity.user.OwnerAct;
import com.datawin.taotaocheche.ui.fragment.biz.GrabHallBiz;
import com.datawin.taotaocheche.ui.fragment.contract.GrabHallContract;

public class GrabHallPresent implements GrabHallContract.Presenter {

    private GrabHallContract.View mView;
    private GrabHallBiz mBiz;

    private int index = 1;
    private List<ClueInfo> clueInfoList = new ArrayList<>();

    private String[] carName = {"不限", "两厢轿车", "三厢轿车", "跑车", "SUV", "MPV", "面包车", "皮卡"};

    @Inject
    public GrabHallPresent(GrabHallContract.View view) {
        this.mView = view;
    }

    @Override
    public void setBiz(BaseBiz biz) {
        this.mBiz = (GrabHallBiz) biz;
    }

    @Override
    public void toOwner() {
        mView.startAct(OwnerAct.class, null);
    }

    @Override
    public void toSelect() {
        mView.startAct(SelcetInfoAct.class, null);
    }

    @Override
    public void getList(SettingInfo settingInfo) {
        mView.loading(true);
        mBiz.getClueList(MyApplication.userInfo.getUserCode(), index, Constants.PAGESIZE, settingInfo.getCityIds(), carName[settingInfo.getCarType()], settingInfo.getYear(), settingInfo.getRoad(),new BaseBiz.Callback<BaseBean<List<ClueInfo>>>() {
            @Override
            public void onSuccess(BaseBean<List<ClueInfo>> o) {
                        clueInfoList.addAll(o.getList());
                        mView.updateList(clueInfoList);
                        if (index > 1) {
                            mView.loadMoreComplete();
                        } else {
                            mView.refreshComplete();
                        }
                        mView.loading(false);
                        mView.hasLoadMore(index * Constants.PAGESIZE >= o.getTotal());
                        index++;
                    }

                    @Override
                    public void onFailure(BaseBean<List<ClueInfo>> o) {
                        if (index > 1) {
                            mView.loadMoreComplete();
                        } else {
                            mView.refreshComplete();
                        }
                        mView.loading(false);
                    }
                });
    }

    @Override
    public void refresh() {
        index = 1;
        clueInfoList.clear();
        mView.updateList(clueInfoList);
    }
}
